package studentdemo.dao;

import studentdemo.modul.Teacher;
import studentdemo.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {
    @Override
    public int insertTeacher(Teacher teacher) {
        String sql = "INSERT INTO Dujx_Teacher11 (djx_Tno11, djx_Tname11, djx_Tsex11, djx_Tage11, djx_Ttitle11, djx_Tphone11) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, teacher.getTno());
            ps.setString(2, teacher.getTname());
            ps.setString(3, teacher.getTsex());
            ps.setInt(4, teacher.getTage());
            ps.setString(5, teacher.getTtitle());
            ps.setString(6, teacher.getTphone());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        String sql = "UPDATE Dujx_Teacher11 SET djx_Tname11=?, djx_Tsex11=?, djx_Tage11=?, djx_Ttitle11=?, djx_Tphone11=? WHERE djx_Tno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, teacher.getTname());
            ps.setString(2, teacher.getTsex());
            ps.setInt(3, teacher.getTage());
            ps.setString(4, teacher.getTtitle());
            ps.setString(5, teacher.getTphone());
            ps.setString(6, teacher.getTno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteTeacher(String tno) {
        String sql = "DELETE FROM Dujx_Teacher11 WHERE djx_Tno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, tno);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Teacher getTeacherById(String tno) {
        String sql = "SELECT * FROM Dujx_Teacher11 WHERE djx_Tno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, tno);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return extractTeacher(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Teacher> getTeachersByName(String name) {
        String sql = "SELECT * FROM Dujx_Teacher11 WHERE djx_Tname11 LIKE ?";
        List<Teacher> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, "%" + name + "%");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractTeacher(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Teacher> getAllTeachers(int page, int pageSize) {
        String sql = "SELECT * FROM Dujx_Teacher11 ORDER BY djx_Tno11 LIMIT ? OFFSET ?";
        List<Teacher> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, pageSize);
            ps.setInt(2, (page - 1) * pageSize);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractTeacher(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getTeacherCount() {
        String sql = "SELECT COUNT(*) FROM Dujx_Teacher11";
        try (Connection conn = DBUtil.getConnection(); Statement st = conn.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private Teacher extractTeacher(ResultSet rs) throws SQLException {
        Teacher t = new Teacher();
        t.setTno(rs.getString("djx_Tno11"));
        t.setTname(rs.getString("djx_Tname11"));
        t.setTsex(rs.getString("djx_Tsex11"));
        t.setTage(rs.getInt("djx_Tage11"));
        t.setTtitle(rs.getString("djx_Ttitle11"));
        t.setTphone(rs.getString("djx_Tphone11"));
        return t;
    }
} 